//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    showLoading: true,
    noMoretip: false,
    page: 0,
    curStatus:'',
    statusArr:[
      {
        statusName:'全部',
        status:''
      }, {
        statusName: '未使用',
        status: '0'
      }, {
        statusName: '已使用',
        status: '1'
      }, {
        statusName: '已失效',
        status: '2'
      }
    ]
  },
  onLoad:function(){
    var that = this;
    app.setNavColor(that);
    that.requestCouponlist();
  },
  statusToggle:function(e){
    var that = this;
    var status = e.currentTarget.dataset.status;
    that.setData({
      curStatus: status,
      page: 0,
      noMoretip: false,
      showLoading: true
    })
    console.log(that.data.curStatus);
    that.requestCouponlist();
  },
  // 优惠券
  toCouponlist: function () {
    wx.navigateTo({
      url: '/subpages/couponList/couponList'
    })
  },
  toShareCoupon: function (e) {
    var id = e.target.dataset.id;
    if(!id){
      return;
    }
    wx.navigateTo({
      url: '/subpages/shareCoupon/shareCoupon?id='+id
    })
  },
  toIndexuse: function () {
    app.backHome();
  },
  toAllgoods: function (e) {
    var id = e.currentTarget.dataset.id;
    var type = e.currentTarget.dataset.type;
    var limit = e.currentTarget.dataset.limit;
    var value = e.currentTarget.dataset.value;
    wx.navigateTo({
      url: '/subpages1/searchList/searchList?id=' + id + '&type=' + type + '&limit=' + limit + '&value=' + value
    })
  },
  requestCouponlist: function () {
    var that = this;
    var data = {};
    var page = that.data.page;
    data.map = 'applet_my_coupon';
    data.page = page;
    data.status = that.data.curStatus;
    console.log(data);
    wx.request({
      url: app.globalData.requestUrl,
      data: data,
      success: function (res) {
        console.log(res.data);
        if (res.data.ec == 200) {
          var allArr = [];
          var initArr = that.data.couponList ? that.data.couponList : [];
          var curArr = res.data.data;
          var lastPageLength = curArr.length;
          if (page > 0) {
            allArr = initArr.concat(curArr);
          } else {
            allArr = res.data.data;
          }
          that.setData({
            couponList: allArr
          })
          if (lastPageLength < 10) {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
          console.log(that.data.couponList);
        } else {
          if (page <= 0) {
            that.setData({
              couponList: [],
              noMoretip: false,
              showLoading: false
            })
          } else {
            that.setData({
              noMoretip: true,
              showLoading: false
            });
          }
        }
      },
      complete: function () {
        wx.hideToast();
        wx.stopPullDownRefresh();
      }
    });
  },
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      page: 0,
      noMoretip: false,
      showLoading: true
    });
    var type = that.data.type;
    that.requestCouponlist(type);
    console.log("下拉刷新");
  },
  onReachBottom: function () {
    var that = this;
    console.log("到达页面底部")
    var isMore = that.data.noMoretip;
    var page = that.data.page;
    var sortType = that.data.curSort;
    var curLng = that.data.curLng;
    var curLat = that.data.curLat;
    page++;
    that.setData({
      page: page
    });
    if (isMore) {
      console.log("已完成或正在加载");
    } else {
      var type = that.data.type;
      that.requestCouponlist(type);
    }
  },
})
